package aar.test.tessocr.wxapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import aar.test.tessocr.EventMessage.EventMessage;
import aar.test.tessocr.base.BaseActivity;
import clx.tessocr.ocreng.R;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	private String appId = "wx8890d06ae86d3400";
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, appId);
        api.handleIntent(getIntent(), this);
		/*if (!EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().register(this);
		}*/
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@SuppressLint("LongLogTag")
	@Override
	public void onResp(BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			switch (resp.errCode) {
				case 0://支付成功
					Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
					EventBus.getDefault().post(new EventMessage().setMessage("shutDownPay"));
					Log.d(TAG, "onResp: resp.errCode = 0   支付成功");
					break;
				case -1://错误，可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
					Toast.makeText(this, "支付错误" + resp.errCode, Toast.LENGTH_SHORT).show();
					Log.d(TAG, "onResp: resp.errCode = -1  支付错误");
					break;
				case -2://用户取消，无需处理。发生场景：用户不支付了，点击取消，返回APP。
					Log.d(TAG, "onResp: resp.errCode = -2  用户取消");
					Toast.makeText(this, "用户取消" + resp.errCode, Toast.LENGTH_SHORT).show();
					break;
			}

			finish();
		}
	}

	/*@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);

	}*/
}